import ky from "ky";

const GET_CONTENTS_URL = "https://act-api-takumi-static.mihoyo.com/content_v2_user/app/16471662a82d418a/getContentList";

type SearchParamsType = {
  appId: string;
  chanId: string;
  pageSize: number;
  page: number;
  langKey: string;
  order: number;
};

const defaultSearchParams = () => ({
  iAppId: "43",
  iChanId: "742",
  iPageSize: 50,
  iPage: 1,
  sLangKey: "zh-cn",
  iOrder: 6,
});

export const getByChanId = async ({ chanId }: Pick<SearchParamsType, "chanId">): Promise<DYsContentPager> => {
  const searchParams = defaultSearchParams();
  searchParams.iChanId = chanId;
  const json: DYsRespWrapper = await ky.get(GET_CONTENTS_URL, { searchParams }).json();
  if (json.retcode === 0) {
    return json.data;
  }
  throw new Error(`Error occurs when get ys content list, retcode: ${json.retcode}, message: ${json.message}`);
};

export const getNations = (): Promise<DYsRespWrapper> =>
  ky.get(GET_CONTENTS_URL, { searchParams: defaultSearchParams() }).json();
